package com.jwthhealth_pub.old.sportfitness.view;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.old.sportfitness.module.SprotCourseInfoModule;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportCourseTrainPresenter;
import com.jwthhealth_pub.old.sportfitness.presenter.ISportCourseTrainPresenterCompl;
import com.jwthhealth_pub.utils.TimeUtil;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 视频训练页面
 * Created by Administrator on 2017/1/9.
 */
public class SportCourseTrainActivity extends Activity {

    private static final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jwth/";

    public static int TITLE = 0;
    public static int UPDATE_READY = 1;
    public static int UPDATA_READY_GONE = 2;
    public static int UPDATA_UPATA_TIME = 3;
    public static int FINISH = 4;

    private static String VIDEOUI = "videoUi";

    @BindView(R.id.layout_surface)
    RelativeLayout layoutSurface;
    @BindView(R.id.tv_current_progress)
    TextView tvCurrentProgress;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.train_describe)
    TextView trainDescribe;
    @BindView(R.id.title_layout)
    RelativeLayout titleLayout;
    @BindView(R.id.train_seekbar)
    SeekBar trainSeekbar;
    @BindView(R.id.train_current_time)
    TextView trainCurrentTime;
    @BindView(R.id.train_max_time)
    TextView trainMaxTime;
    @BindView(R.id.play_btn)
    ImageView playBtn;
    @BindView(R.id.next_btn)
    ImageView nextBtn;
    @BindView(R.id.last_btn)
    ImageView lastBtn;
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.surfaceview)
    SurfaceView surfaceview;
    @BindView(R.id.tv_timer)
    TextView tvTimer;
    @BindView(R.id.tv_jump_rest)
    TextView tvJumpRest;
    @BindView(R.id.tv_ready)
    TextView tvReady;
    @BindView(R.id.time_layout)
    RelativeLayout timeLayout;
    @BindView(R.id.train_title)
    TextView trainTitle;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.max)
    ImageView max;
    @BindView(R.id.max_title)
    TextView maxTitle;
    @BindView(R.id.min)
    ImageView min;
    @BindView(R.id.max_train_seekbar)
    SeekBar maxTrainSeekbar;
    @BindView(R.id.max_train_current_time)
    TextView maxTrainCurrentTime;
    @BindView(R.id.max_play_btn)
    ImageView maxPlayBtn;
    @BindView(R.id.max_last_btn)
    ImageView maxLastBtn;
    @BindView(R.id.max_next_btn)
    ImageView maxNextBtn;
    @BindView(R.id.min_layout)
    RelativeLayout minLayout;
    @BindView(R.id.max_layout)
    RelativeLayout maxLayout;
    @BindView(R.id.max_back)
    ImageView maxBack;

    View controLayout, optLayout;
    @BindView(R.id.max_rest_layout)
    RelativeLayout maxRestLayout;
    @BindView(R.id.tv_max_timer)
    TextView tvMaxTimer;
    @BindView(R.id.tv_max_describe)
    TextView tvMaxDescribe;

    private SprotCourseInfoModule.DataBean data;
    private MediaPlayer mp;
    private SprotCourseInfoModule.DataBean.VideoListBean currentVideo;
    private int videoIndex;

    //暂停倒计时计数器
    private Timer timer = new Timer();
    //    private int readyTimerIndex;//准备时间计数
    private int restTimerIndex;//休息时间计数

    //播放次数计数器
    private int playCount;

    boolean isFirstIn = true;
    private CountDownTimer countDownTimer;
    private boolean isTimer;

    private long pauseStart; // 暂停结束时间，用于计算训练时间
    private long pauseTime;//暂停时间，用于计算训练时间
    private long playStartTime; // 开始计时的时间点
    private long updateStringtime;// 训练计数器开始时间
    private long updatetimeUI;//用于页面更新
    private ISportCourseTrainPresenter iSportCourseTrainPresenter;
    private boolean isPause;
    private int surfaceViewHeight;
    private int layoutSurfaceHei;
    private int trainTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_course_train);
        ButterKnife.bind(this);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        controLayout = findViewById(R.id.controll_layout);
        optLayout = findViewById(R.id.opt_layout);

        data = (SprotCourseInfoModule.DataBean) getIntent().getExtras().getSerializable(Constant.SPORT_DATA);

        iSportCourseTrainPresenter = new ISportCourseTrainPresenterCompl(this);

        File dir = new File(path + data.getId());
        if (!dir.exists()) {
            iSportCourseTrainPresenter.unZip(path + "zip" + data.getId(), path + data.getId());
        } else {
            Log.d("SportCourseTrainActivit", "直接播放");
            initTitle();
//            initTimer();
            initVideo();
            initSeekbar();
        }

    }

    public void initSeekbar() {
        trainSeekbar.setMax(data.getVideo_list().size());
        maxTrainSeekbar.setMax(data.getVideo_list().size());
        updateSeekbar();
    }

    private void updateSeekbar() {
        trainSeekbar.setProgress(videoIndex + 1);
        maxTrainSeekbar.setProgress(videoIndex + 1);
    }

    private void initTimer() {
        isFirstIn = false;
//        timer.schedule(getReadyTimerTask(), 1000, 1000);
    }

    public void initVideo() {

        videoIndex = 0;
        currentVideo = data.getVideo_list().get(0);

        mp = new MediaPlayer();
        setVideoListener(mp);

        SurfaceHolder surfaceHolder = surfaceview.getHolder();
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mp.setDisplay(surfaceHolder);
                try {
                    if (isFirstIn) {
                        mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
//                    mp.setLooping(true);//循环播放
                        mp.prepareAsync();
                    }
                } catch (IOException e) {
                    Log.e("SportCourseListActivit", e.toString());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                Log.d("SportCourseTrainActivit", "surfaceview change");
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                Log.d("SportCourseTrainActivit", "surfaceholder destory");
            }
        });
    }


    /**
     * 3s准备时间任务
     *
     * @return
     */
    private TimerTask getReadyTimerTask() {
        final TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {

//                readyTimerIndex--;
//                // TODO: 2017/1/9 更新页面ui
//                Message message = new Message();
//                message.what = 1;
//                Bundle bundle = new Bundle();
//                bundle.putString(VIDEOUI, String.valueOf(readyTimerIndex));
//                message.setData(bundle);
////                tvReady.setText(String.valueOf(readyTimerIndex));
//                if (readyTimerIndex == 0) {
////                    tvReady.setText(getString(R.string.));
//                    bundle.putString(VIDEOUI, getString(R.string.sport_ready_start));
//                }
//
//                if (readyTimerIndex < 0) {
//                    timer.cancel();
//                    message.what = 2;
//                }
//
//                mHandler.sendMessage(message);
            }
        };

        return timerTask;
    }


    private void setVideoListener(final MediaPlayer mp) {
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

                //累计当前视频播放次数
                playCount++;
                if (String.valueOf(playCount).equals(currentVideo.getDegree())) {
                    initTimer();
                    updateSeekbar();
                    isTimer = true;
                    final int resttime = Integer.parseInt(currentVideo.getRest_time());
                    countDownTimer = new CountDownTimer(resttime * 1000, 1000) {
                        @Override
                        public void onTick(long l) {
                            tvTimer.setText(String.valueOf("休息一下: " + l / 1000));
                            tvTimer.setTextSize(20);
                            if ((getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)) {
                                tvMaxTimer.setVisibility(View.VISIBLE);
                                tvMaxDescribe.setVisibility(View.VISIBLE);
                                maxRestLayout.setVisibility(View.VISIBLE);
                                tvMaxTimer.setText(String.valueOf(l / 1000));
                            }
                        }

                        @Override
                        public void onFinish() {
                            videoIndex++;

                            long finishTime = System.currentTimeMillis();
                            int trainTime = (int) (finishTime - playStartTime);

                            if (videoIndex >= data.getVideo_list().size() || trainTime / 1000 > Integer.parseInt(data.getTime_long())) {
//                                Log.d("SportCourseTrainActivit", "所有动作训练完毕");
//                                LoginModel.MsgDataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
//
//                                iSportCourseTrainPresenter.commit(userinfoModel.getId(), data.getId(), String.valueOf(trainTime));

                                showFinishDialog(trainTime);

                                return;
                            }
                            currentVideo = data.getVideo_list().get(videoIndex);
                            initTitle();
                            mp.reset();
                            try {
                                mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
                                mp.prepareAsync();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            isTimer = false;

                            tvMaxTimer.setVisibility(View.GONE);
                            tvMaxDescribe.setVisibility(View.GONE);
                            maxRestLayout.setVisibility(View.GONE);
                        }
                    };
                    countDownTimer.start();
//                    mp.setLooping(true);//循环播放
                } else {
                    tvTimer.setTextSize(39);
                    tvTimer.setText(playCount + 1 + "/" + currentVideo.getDegree());
                    mp.start();
                    tvMaxTimer.setVisibility(View.GONE);
                    tvMaxDescribe.setVisibility(View.GONE);
                    maxRestLayout.setVisibility(View.GONE);
                }
//                videoIndex++;
//                currentVideo = data.getVideo_list().get(videoIndex);

//                try {
//                    mp.reset();
//                    mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
////                    mp.setLooping(true);//循环播放
//                    mp.prepareAsync();
//                } catch (IOException e) {
//                    Log.d("SportCourseListActivity", e.toString());
//                }
//
//                Log.d("SportCourseTrainActivit", "complete");
            }
        });

        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {

                if (videoIndex == 0) {
                    playStartTime = System.currentTimeMillis();
                    initUpdate();
                }

                playCount = 0;
                trainTitle.setText(currentVideo.getName());
                //max
                maxTitle.setText(String.valueOf(videoIndex + 1) + "/" + data.getVideo_list().size() + " " + currentVideo.getName());
                mp.start();
                playBtn.setBackground(getResources().getDrawable(R.mipmap.btn_pause));
                maxPlayBtn.setBackground(getResources().getDrawable(R.mipmap.btn_pause));
                tvTimer.setTextSize(39);
                tvTimer.setText("1" + "/" + currentVideo.getDegree());
//                mp.pause();
//                initTimer();
            }
        });
    }

    private void initUpdate() {
        updateStringtime = System.currentTimeMillis();
        final TimerTask updateTimerTask = new TimerTask() {

            @Override
            public void run() {
                try {
                    if (!isPause) {
                        updatetimeUI = System.currentTimeMillis() - updateStringtime;
                        mHandler.sendEmptyMessage(UPDATA_UPATA_TIME);
                    }
                } catch (Exception e) {
                    Log.e("SportCourseTrainActivit", e.toString());
                }
            }
        };

        timer = new Timer();
        timer.schedule(updateTimerTask, 500, 1000);
    }

    public void initTitle() {
        //min
        title.setText("/" + data.getVideo_list().size() + " " + data.getName());
        tvCurrentProgress.setText(String.valueOf(videoIndex + 1));
    }

    @OnClick({R.id.play_btn, R.id.next_btn, R.id.last_btn, R.id.time_layout, R.id.max, R.id.max_play_btn, R.id.max_last_btn, R.id.max_next_btn, R.id.min
            , R.id.back, R.id.max_back, R.id.max_rest_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play_btn:
                playClick();
                break;
            case R.id.last_btn:
                lastClick();
                break;
            case R.id.next_btn:
                nextBtn();
                break;
            case R.id.time_layout:
                // TODO: 2017/1/9 暂停休息
                break;
            case R.id.max:
                changeMediaUiSize();
                break;
            case R.id.max_play_btn:
                playClick();
                break;
            case R.id.max_last_btn:
                lastClick();
                break;
            case R.id.max_next_btn:
                nextBtn();
                break;
            case R.id.min:
                changeMediaUiSize();
                break;
            case R.id.back:
                clickfinish();
                break;
            case R.id.max_back:
                clickfinish();
                break;
            case R.id.max_rest_layout:
                maxRestLayout.setVisibility(View.GONE);
                tvMaxTimer.setVisibility(View.GONE);
                tvMaxDescribe.setVisibility(View.GONE);
                isTimer = false;
                lastClick();
                break;
            default:
                break;
        }
    }

    private void nextBtn() {
        //上一个视频
//                mp.pause();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        if ((videoIndex - 1) < 0) {
            return;
        }
        videoIndex--;
        currentVideo = data.getVideo_list().get(videoIndex);
        initTitle();
        updateSeekbar();
        mp.reset();
        try {
            mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
            mp.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void lastClick() {
        //下一个视频
//                mp.pause();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        if ((videoIndex + 1) >= data.getVideo_list().size()) {
            return;
        }
        videoIndex++;
        currentVideo = data.getVideo_list().get(videoIndex);
        initTitle();
        updateSeekbar();
        mp.reset();
        try {
            mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
            mp.prepareAsync();
        } catch (IOException e) {
            Log.d("SportCourseTrainActivit", "e:" + e);
        }
    }

    public void resetPlay(){
        currentVideo = data.getVideo_list().get(videoIndex);
        initTitle();
        updateSeekbar();
        mp.reset();
        try {
            mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
            mp.prepareAsync();
        } catch (IOException e) {
            Log.d("SportCourseTrainActivit", "e:" + e);
        }
    }

    private void playClick() {
        if (isTimer) {
            Log.d("SportCourseTrainActivit", "isTimer is false");
            return;
        }
        if (mp.isPlaying()) {
            isPause = true;
            mp.pause();
            pauseStart = System.currentTimeMillis();
            playBtn.setBackground(getResources().getDrawable(R.mipmap.btn_play));
            maxPlayBtn.setBackground(getResources().getDrawable(R.mipmap.btn_play));
        } else {
            isPause = false;
            mp.start();
            pauseTime = System.currentTimeMillis() - pauseStart;
            playBtn.setBackground(getResources().getDrawable(R.mipmap.btn_pause));
            maxPlayBtn.setBackground(getResources().getDrawable(R.mipmap.btn_pause));
        }
    }

    private void changeMediaUiSize() {
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            //min
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, surfaceViewHeight);
            surfaceview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            Log.d("SportCourseTrainActivit", "layoutParams.height:" + layoutParams.height);
            surfaceview.setLayoutParams(layoutParams);
            layoutSurface.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,layoutSurfaceHei));
            maxLayout.setVisibility(View.GONE);
            minLayout.setVisibility(View.VISIBLE);
            controLayout.setVisibility(View.VISIBLE);
            optLayout.setVisibility(View.VISIBLE);
            titleLayout.setVisibility(View.VISIBLE);
        } else {
            //max
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
            surfaceview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
            surfaceview.setLayoutParams(layoutParams);
            layoutSurface.setLayoutParams(layoutParams);//这句必须写
            maxLayout.setVisibility(View.VISIBLE);
            minLayout.setVisibility(View.GONE);
            controLayout.setVisibility(View.GONE);
            optLayout.setVisibility(View.GONE);
            titleLayout.setVisibility(View.GONE);
        }
    }

    public void progressbar() {
        if (progressbar.getVisibility() == View.VISIBLE) {
            progressbar.setVisibility(View.VISIBLE);
        } else {
            progressbar.setVisibility(View.GONE);
        }
    }

    /*
         这里使用了 android.support.v7.app.AlertDialog.Builder
         可以直接在头部写 import android.support.v7.app.AlertDialog
         那么下面就可以写成 AlertDialog.Builder
         */

    /**
     * 训练完成提示
     *
     * @param trainTime
     */
    private void showFinishDialog(final int trainTime) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("您已经完成训练");
//        builder.setMessage("");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.d("SportCourseTrainActivit", "所有动作训练完毕");
                LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                if (userinfoModel == null) {
                    Toast.makeText(SportCourseTrainActivity.this, "登陆状态异常", Toast.LENGTH_SHORT).show();
                    finish();
                    return;
                }

                iSportCourseTrainPresenter.commit(userinfoModel.getId(), data.getId(), String.valueOf(trainTime), userinfoModel.getAndroidtoken());
            }
        });
        builder.show();
    }

    /**
     * 训练未完成提示
     */
    private void showExitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("您未完成训练");
        builder.setMessage(getResources().getString(R.string.sport_train_exit_alert));
        builder.setNegativeButton("取消", null);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.show();
    }

    private void clickfinish() {
        long finishTime = System.currentTimeMillis();
        trainTime = (int) (finishTime - playStartTime);

        if (videoIndex >= data.getVideo_list().size() || trainTime / 1000 > Integer.parseInt(data.getTime_long())) {
            showFinishDialog(trainTime);
        } else {
            showExitDialog();
        }
    }

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    Log.d("SportCourseTrainActivit", "解压后播放");
                    initTitle();
//            initTimer();
                    initVideo();
                    initSeekbar();
                    break;
                case 1:
                    String data = (String) msg.getData().get(VIDEOUI);
                    if (data.equals(getString(R.string.sport_ready_start))) {
                        tvJumpRest.setVisibility(View.GONE);
                    }
                    tvTimer.setText(data);
                    break;
                case 2:
                    timeLayout.setVisibility(View.GONE);
                    //播放视频
                    mp.start();
//                    timer.cancel();
                    break;
                case 3:
                    trainCurrentTime.setText(TimeUtil.getString((int) (updatetimeUI / 1000)));
                    maxTrainCurrentTime.setText(TimeUtil.getString((int) (updatetimeUI / 1000)));
                    break;
                case 4:
                    progressbar();
                    Toast.makeText(SportCourseTrainActivity.this, "本次训练已完成", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        surfaceViewHeight = layoutSurface.getMeasuredHeight();

        super.onWindowFocusChanged(hasFocus);
        double width = surfaceview.getWidth();
        RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) surfaceview.getLayoutParams();
        layoutSurfaceHei = layoutSurface.getLayoutParams().height;
        int surfaceHei = (int) (width * 1.178);
        params.height= surfaceHei;//设置当前控件布局的高度
        Log.d("SportCourseTrainActivit", "surfaceHei:" + surfaceHei);
        Log.d("SportCourseTrainActivit", "params.height:" + params.height);
        surfaceview.setLayoutParams(params);//将设置好的布局参数应用到控件中
        surfaceViewHeight = surfaceHei;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            clickfinish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        initTimer();
        playClick();
//        if (mp != null && mp.isPlaying()) {
//            isFirstIn = false;
//            mp.pause();
//        }
        if (isFirstIn != true) {
//            timer.cancel();
//            mp.start();
        }

        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

//        playClick();
//        Log.d("SportCourseTrainActivit", "onresume");
//        Log.d("SportCourseTrainActivit", "videoIndex:" + videoIndex);
        if(videoIndex == 1) {
//            lastClick();
            nextBtn();
            return;
        }
        if (mp != null && !mp.isPlaying() && currentVideo != null && !isFirstIn) {
//            resetPlay();
            lastClick();
            if ((videoIndex + 1) >= data.getVideo_list().size()) {
                //最后一个视频
                Log.d("SportCourseTrainActivit", "最后一个视频");
                nextBtn();
                lastClick();
                return;
            }
            if ((videoIndex - 1) < 0) {
                //第一个视频
                Log.d("SportCourseTrainActivit", "第一个视频");
                lastClick();
                nextBtn();
                return;
            }
            nextBtn();
////            mp.start();
//            mp.reset();
//            try {
//                mp.setDataSource(path + data.getId() + "/" + currentVideo.getVideo_name());
//                mp.prepareAsync();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        timer.cancel();
        mp.release();
    }
}
